#!/usr/bin/python

__author__ = "Jimmy Saw"
__copyright__ = "Copyright 2011, My Auto-annotator"
__credits__ = "Jimmy Saw"
__email__ = "jimmysaw@gmail.com"

"""
TO DO: 
Usage: python auto_anno.py annofile.txt seqfile.fasta

"""

import sys
import re
from Bio import SeqIO

#Formatting for 4 significant figures (for decimals)
def fmt(f):
    st = '{0:.4}'.format(f)
    return st

#Indicator
def indicate():
    st = "<-------------------------------------"
    return st

#Check start sites begins
annofile = sys.argv[1]
#seqfile = sys.argv[2]
af = open(annofile, "rU")
#sf = open(seqfile, "rU")
#rec = SeqIO.read(sf, "fasta")
lines = af.readlines()
num = len(lines)

i = 0

cds_list = []
cds_starts = []
cds_stops = []
rna_list = []
rna_defs = []
rna_start_list = []
rna_stop_list = []

while i < num:
    curr_line = lines[i].split('\t')
    curr_prodigal_id = curr_line[0]
    curr_locus_tag = curr_line[1]
    curr_feat_type = curr_line[2]
    curr_start = int(curr_line[3])
    curr_stop = int(curr_line[4])
    curr_frame = curr_line[5]
    curr_blast_status = curr_line[6]
    curr_blast_evalue = curr_line[7]
    curr_tophit_id = curr_line[8]
    curr_tophit_sim = curr_line[9]
    curr_tophit_acc = curr_line[10]
    curr_tophit_gi = curr_line[11]
    curr_blast_qstart = curr_line[12]
    curr_blast_qstop = curr_line[13]
    curr_hitlength = curr_line[14]
    curr_blast_mstart = curr_line[15]
    curr_blast_mstop = curr_line[16]
    curr_hit_def = curr_line[17]
    curr_hit_org = curr_line[18]
    curr_COG_status = curr_line[19]
    curr_COG_len = curr_line[20]
    curr_COG_evalue = curr_line[21]
    curr_COG_pid = curr_line[22]
    curr_COG_psim = curr_line[23]
    curr_COG_start = curr_line[24]
    curr_COG_stop = curr_line[25]
    curr_COG_id = curr_line[26]
    curr_COG_cat = curr_line[27]
    curr_COG_def = curr_line[28]
    curr_gene_name = curr_line[29]
    curr_gene_name_fixed = curr_line[30]
    curr_cluster_def = curr_line[31]
    curr_cluster_org = curr_line[32]

    if curr_feat_type == "CDS":
        cds_list.append(curr_prodigal_id)
        cds_starts.append(curr_start)
        cds_stops.append(curr_stop)

    elif curr_feat_type == "rRNA":
        rna_list.append(curr_locus_tag)
        rna_defs.append(curr_hit_def)
        rna_start_list.append(curr_start)
        rna_stop_list.append(curr_stop)
        print curr_locus_tag, curr_feat_type, curr_start, curr_stop, curr_hit_def

    elif curr_feat_type == "tRNA":
        rna_list.append(curr_locus_tag)
        rna_defs.append(curr_hit_def)
        rna_start_list.append(curr_start)
        rna_stop_list.append(curr_stop)
        print curr_locus_tag, curr_feat_type, curr_start, curr_stop, curr_hit_def
    else:
        pass
    i += 1

rna_count = len(rna_start_list)

print "Total RNAs found:", rna_count

for i, x in enumerate(rna_list):
    for j, k in enumerate(cds_list):
        if cds_starts[j] < rna_start_list[i] < cds_stops[j] or cds_starts[j] < rna_stop_list[i] < cds_stops[j]:
            print cds_list[j], cds_starts[j], cds_stops[j], "\t", "Might need to be removed! Overlaps with", rna_list[i], rna_defs[i], rna_start_list[i], rna_stop_list[i]
        elif cds_starts[j] < rna_start_list[i] < cds_stops[j] and cds_starts[j] < rna_stop_list[i] < cds_stops[j]:
            print cds_list[j], cds_starts[j], cds_stops[j], "\t", "Need to be removed! Contained within", rna_list[i], rna_defs[i], rna_start_list[i], rna_stop_list[i]

for i, x in enumerate(cds_list):
    for j, k in enumerate(rna_list):
        if rna_start_list[j] < cds_starts[i] < rna_stop_list[j] or rna_start_list[j] < cds_stops[i] < rna_stop_list[j]:
            print cds_list[i], cds_starts[i], cds_stops[i], "\t", "Might need to be removed! Overlaps with", rna_list[j], rna_defs[j], rna_start_list[j], rna_stop_list[j] 
        elif rna_start_list[j] < cds_starts[i] < rna_stop_list[j] and rna_start_list[j] < cds_stops[i] < rna_stop_list[j]:
            print cds_list[i], cds_starts[i], cds_stops[i], "\t", "Need to be removed! Contained within", rna_list[j], rna_defs[j], rna_start_list[j], rna_stop_list[j] 

af.close()
